package com.xindaoapp.happypet.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.BaseEntity;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.ProgressHUD;

public class PayBackActivity extends UMBaseActivity implements OnClickListener {
    @ViewInject(R.id.tittle_shop_content)
    private TextView tittle_shop_content;

    @ViewInject(R.id.tittle_shop_cancel)
    private ImageView tittle_shop_cancel;

    // @ViewInject(R.id.group)
    // private RadioGroup group;

    @ViewInject(R.id.tuikuantuihuo)
    private RelativeLayout tuikuantuihuo;
    @ViewInject(R.id.tuikuan)
    private RelativeLayout tuikuan;
    @ViewInject(R.id.check_result)
    private RelativeLayout check_result;


    @ViewInject(R.id.tuikuan_result)
    private TextView tuikuan_result;
    @ViewInject(R.id.money)
    private TextView money;
    @ViewInject(R.id.et)
    private EditText et;


    @ViewInject(R.id.submit)
    private Button submit;

    @ViewInject(R.id.tuikuan_checkbox)
    private CheckBox tuikuan_checkbox;
    @ViewInject(R.id.tuikuantuihuo_checkbox)
    private CheckBox tuikuantuihuo_checkbox;


    private String totalprice;
    private String orderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payback);
        totalprice = getIntent().getStringExtra("price");
        orderId = getIntent().getStringExtra("orderId");
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        money.setText("￥：" + totalprice);
        tittle_shop_content.setText("申请退款");
        tittle_shop_cancel.setOnClickListener(this);
        submit.setOnClickListener(this);
        tuikuantuihuo.setOnClickListener(this);
        tuikuan.setOnClickListener(this);
        check_result.setOnClickListener(this);
        tuikuantuihuo_checkbox.setChecked(true);
    }

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private Button ok;
    private ImageView close;
    private AlertDialog dialog;
    private String type;
    private String comment;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tittle_shop_cancel:
                finish();
                break;
            case R.id.submit:
                if (tuikuan_checkbox.isChecked()) {
                    type = "1";
                }
                if (tuikuantuihuo_checkbox.isChecked()) {
                    type = "2";
                }
                if (TextUtils.isEmpty(type)) {
                    Toast.makeText(getApplicationContext(), "请选择退款类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(result)) {
                    Toast.makeText(getApplicationContext(), "请选择退款原因", Toast.LENGTH_SHORT).show();
                    return;
                }
                comment = et.getText().toString().trim();
                drawBack();
                break;
            case R.id.tuikuantuihuo:
                tuikuan_checkbox.setChecked(false);
                tuikuantuihuo_checkbox.setChecked(true);
                break;
            case R.id.tuikuan:
                tuikuan_checkbox.setChecked(true);
                tuikuantuihuo_checkbox.setChecked(false);
                break;
            case R.id.check_result:
                AlertDialog.Builder builder = new Builder(this);
                View view = View.inflate(this, R.layout.dialog_choose_result, null);
                LayoutParams params = new LayoutParams(200, 300);
                view.setLayoutParams(params);
                tv1 = (TextView) view.findViewById(R.id.tv1);
                tv2 = (TextView) view.findViewById(R.id.tv2);
                tv3 = (TextView) view.findViewById(R.id.tv3);
                ok = (Button) view.findViewById(R.id.ok);
                ok = (Button) view.findViewById(R.id.ok);
                close = (ImageView) view.findViewById(R.id.close);

                tv1.setOnClickListener(new ChooseResultListener());
                tv2.setOnClickListener(new ChooseResultListener());
                tv3.setOnClickListener(new ChooseResultListener());
                ok.setOnClickListener(new ChooseResultListener());
                close.setOnClickListener(new ChooseResultListener());

                dialog = builder.create();
                // dialog.setView(view);
                tv1.setSelected(true);
                dialog.show();
                dialog.getWindow().setContentView(view);
                dialog.setOnCancelListener(new OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        dialog.dismiss();
                    }
                });
                break;
            default:
                break;
        }
    }

    private void drawBack() {
        RequestParams params = new RequestParams();
        params.put("orderID", orderId);
        params.put("userID", UserState.getUserUid());
        params.put("type", type);
        params.put("reason", result);
        params.put("comment", comment);
        final ProgressHUD ProgressDialog =
                ProgressHUD.show(PayBackActivity.this, "正在加载...", true, true, null);
        AddClient.post(Address.PAYBACK, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                try {
                    BaseEntity parse = JSON.parseObject(content, BaseEntity.class);
                    if (parse.getStatus().equals("1")) {
                        Toast.makeText(getApplicationContext(), parse.getMsg(), Toast.LENGTH_SHORT)
                                .show();
                        finish();
                    } else {
                        Toast.makeText(getApplicationContext(), "申请失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFinish() {
                if (null != ProgressDialog && ProgressDialog.isShowing()) {
                    ProgressDialog.dismiss();
                }
                super.onFinish();
            }

            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
            }
        });

    }

    private String result = "";

    private class ChooseResultListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv1:
                    tv1.setSelected(true);
                    tv2.setSelected(false);
                    tv3.setSelected(false);
                    break;
                case R.id.tv2:
                    tv1.setSelected(false);
                    tv2.setSelected(true);
                    tv3.setSelected(false);
                    break;
                case R.id.tv3:
                    tv1.setSelected(false);
                    tv2.setSelected(false);
                    tv3.setSelected(true);
                    break;
                case R.id.ok:
                    if (tv1.isSelected()) {
                        result = "商品问题";
                    } else if (tv2.isSelected()) {
                        result = "运费问题";
                    } else if (tv3.isSelected()) {
                        result = "个人原因";
                    }
                    tuikuan_result.setText(result);
                    dialog.dismiss();
                    break;
                case R.id.close:
                    dialog.dismiss();
                    break;
                default:
                    break;
            }
        }
    }
}
